﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Project01
{
 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        MainMenuSystemComponent MainMenu;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            //Components.Add(monster = new MonsterComponent(this));
            Components.Add(MainMenu = new MainMenuSystemComponent(this));

            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
        }
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            

            //Note:Check Collision follow methods PixelCollision
            //Check Collision between player and monster
            // Check Collision between player and bullets of monster
           // if (Time.TotalMilliseconds > 0)
           // {
           //     Time += gameTime.ElapsedGameTime;
           //     if (Time >= TimeSpan.FromSeconds(3))
           //         Time = TimeSpan.Zero;
           // }
           // if (PlayerwithMonster_Collision()  &&Time ==TimeSpan.Zero
           //     || PlayerwithBulletsMonster_Collision() && Time == TimeSpan.Zero)
           // {

           //     Time += gameTime.ElapsedGameTime;

           // }
          
           //MonsterwithBulletsPlayer_Collision();//check Collision between bullets of player and monster
            base.Update(gameTime);
        }
        //private bool PlayerwithMonster_Collision()
        //{
        //    for (int i = 0; i < monster.MList.Count; i++)
        //    {
        //        if(player.Player.Sprite.rectangle(player.Player.Sprite.Position).Intersects(monster.MList[i].Sprite.rectangle(monster.MList[i].Sprite.Position)))
        //        {
        //        if (PixelCollision(player.Player.Sprite.Texture, monster.MList[i].Sprite.Texture,
        //               player.Player.Sprite.rectangle(player.Player.Sprite.Position), monster.MList[i].Sprite.rectangle(monster.MList[i].Sprite.Position)))
        //        {

        //            return true;
        //        }
                   
        //        }
                
        //    }
        //    return false;
        //}
        //private bool PlayerwithBulletsMonster_Collision()
        //{
        //    for (int i = 0; i < monster.MList.Count; i++)
        //    {
        //        for (int j = 0; j < monster.MList[i].Mbullets.Count; j++)
        //        {
        //            if (player.Player.Sprite.rectangle(player.Player.Sprite.Position).Intersects(monster.MList[i].Mbullets[j].Sprite.rectangle(monster.MList[i].Mbullets[j].Sprite.Position)))
        //            {
        //                if (PixelCollision(player.Player.Sprite.Texture, monster.MList[i].Mbullets[j].Sprite.Texture,
        //                       player.Player.Sprite.rectangle(player.Player.Sprite.Position), monster.MList[i].Mbullets[j].Sprite.rectangle(monster.MList[i].Mbullets[j].Sprite.Position)))
        //                {

        //                    return true;
        //                }
        //            }
        //        }

        //    }
        //    return false;
        //}
        //private bool MonsterwithBulletsPlayer_Collision()
        //{
        //    for (int i = 0; i < player.Player.bullets.Count; i++)
        //    {
        //        for (int j = 0; j < monster.MList.Count; j++)
        //        {
        //            if (monster.MList[j].Type != 6)
        //            {
        //                if (player.Player.bullets[i].Sprite.rectangle(player.Player.bullets[i].Sprite.Position).Intersects(monster.MList[j].Sprite.rectangle(monster.MList[j].Sprite.Position)))
        //                {
        //                    // if (PixelCollision(monster.MList[j].Sprite.Texture, player.Player.bullets[i].Sprite.Texture,
        //                    //  monster.MList[j].Sprite.rectangle(monster.MList[j].Sprite.Position), player.Player.bullets[i].Sprite.rectangle(player.Player.bullets[i].Sprite.Position)))
        //                    // {
        //                    monster.MList[j].SetHealth(player.Player.bullets[i].Damage);
        //                    monster.MList[j].Sprite.Color = Color.Red;
        //                    player.Player.bullets[i] = null;
        //                    player.Player.bullets.RemoveAt(i);

        //                    return true;
        //                    //  }
        //                }
        //                else
        //                {
        //                    monster.MList[j].Sprite.Color = Color.White;
        //                }
        //            }
        //        }
        //    }
        //    return false;
        //}
        //private bool PixelCollision(Texture2D sprite1, Texture2D sprite2, Rectangle player, Rectangle enemy)
        //{

        //    Color[] colorData1 = new Color[sprite1.Width * sprite1.Height];

        //    Color[] colorData2 = new Color[sprite2.Width * sprite2.Height];

        //    sprite1.GetData<Color>(colorData1);

        //    sprite2.GetData<Color>(colorData2);

        //    int top, bottom, left, right;



        //    top = Math.Max(player.Top, enemy.Top);

        //    bottom = Math.Min(player.Bottom, enemy.Bottom);

        //    left = Math.Max(player.Left, enemy.Left);

        //    right = Math.Min(player.Right, enemy.Right);



        //    for (int y = top; y < bottom; y++)
        //    {

        //        for (int x = left; x < right; x++)
        //        {

        //            Color A = colorData1[(y - player.Top) * (player.Width) + (x - player.Left)];

        //            Color B = colorData2[(y - enemy.Top) * (enemy.Width) + (x - enemy.Left)];



        //            if ((A.A != 0 && B.A != 0) )
        //            {
        //                //Console.WriteLine("x");
        //                return true;
        //            }

        //        }

        //    }

        //    return false;

        //}
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);


            base.Draw(gameTime);
        }
    }
}
